package cn.csc.module.product.controller.admin.spuattr;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.spuattr.vo.*;
import cn.csc.module.product.convert.spuattr.SpuAttrValueConvert;
import cn.csc.module.product.dal.dataobject.spuattr.SpuAttrValueDO;
import cn.csc.module.product.service.spuattr.SpuAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商品规格属性关联信息")
@RestController
@RequestMapping("/product/spu-attr-value")
@Validated
public class SpuAttrValueController {

    @Resource
    private SpuAttrValueService spuAttrValueService;

    @PostMapping("/create")
    @ApiOperation("创建商品规格属性关联信息")
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:create')")
    public CommonResult<BigInteger> createSpuAttrValue(@Valid @RequestBody SpuAttrValueCreateReqVO createReqVO) {
        return success(spuAttrValueService.createSpuAttrValue(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品规格属性关联信息")
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:update')")
    public CommonResult<Boolean> updateSpuAttrValue(@Valid @RequestBody SpuAttrValueUpdateReqVO updateReqVO) {
        spuAttrValueService.updateSpuAttrValue(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品规格属性关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:delete')")
    public CommonResult<Boolean> deleteSpuAttrValue(@RequestParam("id") BigInteger id) {
        spuAttrValueService.deleteSpuAttrValue(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品规格属性关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:query')")
    public CommonResult<SpuAttrValueRespVO> getSpuAttrValue(@RequestParam("id") BigInteger id) {
        SpuAttrValueDO spuAttrValue = spuAttrValueService.getSpuAttrValue(id);
        return success(SpuAttrValueConvert.INSTANCE.convert(spuAttrValue));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品规格属性关联信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:query')")
    public CommonResult<List<SpuAttrValueRespVO>> getSpuAttrValueList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuAttrValueDO> list = spuAttrValueService.getSpuAttrValueList(ids);
        return success(SpuAttrValueConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品规格属性关联信息分页")
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:query')")
    public CommonResult<PageResult<SpuAttrValueRespVO>> getSpuAttrValuePage(@Valid SpuAttrValuePageReqVO pageVO) {
        PageResult<SpuAttrValueDO> pageResult = spuAttrValueService.getSpuAttrValuePage(pageVO);
        return success(SpuAttrValueConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品规格属性关联信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-attr-value:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuAttrValueExcel(@Valid SpuAttrValueExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuAttrValueDO> list = spuAttrValueService.getSpuAttrValueList(exportReqVO);
        // 导出 Excel
        List<SpuAttrValueExcelVO> datas = SpuAttrValueConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品规格属性关联信息.xls", "数据", SpuAttrValueExcelVO.class, datas);
    }

}
